package com.yanqu.road.dao.impl.activity.lottery;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.lottery.UserActivityLotteryGuaranteeData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserActivityLotteryGuaranteeDataDaoImpl extends TempDataObjectDao<UserActivityLotteryGuaranteeData> {

    public static String getUserActivityLotteryGuaranteeDataMapSql = "select * from `t_u_activity_lottery_user_guarantee_data` where `userId`=?";
    public Map<Integer,UserActivityLotteryGuaranteeData> getUserActivityLotteryGuaranteeDataMap(long userId) {
        Map<Integer,UserActivityLotteryGuaranteeData> guaranteeDataMap= new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserActivityLotteryGuaranteeDataMapSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityLotteryGuaranteeData parse = parse(rs);
                guaranteeDataMap.put(parse.getType(),parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return guaranteeDataMap;
    }

    public static String getUserActivityLotteryGuaranteeDataSql = "select * from `t_u_activity_lottery_user_guarantee_data` where `userId`=? and `type`=?";
    public UserActivityLotteryGuaranteeData getUserActivityLotteryGuaranteeData(long userId,int type) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserActivityLotteryGuaranteeDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, type);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserActivityLotteryGuaranteeDataSql = "update `t_u_activity_lottery_user_guarantee_data` set drawCount=?,getItemMap=? where userId=? and type=?";
    @Override
    protected boolean updateData(UserActivityLotteryGuaranteeData userActivityLotteryGuaranteeData) {
        userActivityLotteryGuaranteeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryGuaranteeData.getDrawCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userActivityLotteryGuaranteeData.getGetItemMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityLotteryGuaranteeData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryGuaranteeData.getType()));
        if (getDb().executeNoneQuery(updateUserActivityLotteryGuaranteeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserActivityLotteryGuaranteeDataSql = "insert into `t_u_activity_lottery_user_guarantee_data`(userId,type,drawCount,getItemMap) values (?,?,?,?)";
    @Override
    protected boolean addData(UserActivityLotteryGuaranteeData userActivityLotteryGuaranteeData) {
        userActivityLotteryGuaranteeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userActivityLotteryGuaranteeData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryGuaranteeData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryGuaranteeData.getDrawCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userActivityLotteryGuaranteeData.getGetItemMap())));
        if (getDb().executeNoneQuery(insertUserActivityLotteryGuaranteeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserActivityLotteryGuaranteeData parseData(ResultSet rs) throws SQLException {
        UserActivityLotteryGuaranteeData userActivityLotteryGuaranteeData = new UserActivityLotteryGuaranteeData();
        userActivityLotteryGuaranteeData.setUserId(rs.getLong("userId"));
        userActivityLotteryGuaranteeData.setType(rs.getInt("type"));
        userActivityLotteryGuaranteeData.setDrawCount(rs.getInt("drawCount"));
        userActivityLotteryGuaranteeData.setGetItemMap(JSON.parseObject(rs.getString("getItemMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));

        return userActivityLotteryGuaranteeData;
    }

}
